package com.cham.pdm.controller;

import com.cham.enums.ChamSysCodeEnum;
import com.cham.pdm.service.UserService;
import com.cham.user.dto.UserInfo;
import com.cham.user.entity.User;
import com.cham.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Howl-Xu
 * @version 1.0.0
 * @Description: 请描述一下这个类文件吧
 * @createTime 2020年03月14日 11:30:00
 */
@Controller
@RequestMapping("/pdm")
public class PdmController {
    @Autowired
    private UserService userService;

    @GetMapping("/isUserExist")
    @ResponseBody
    public boolean isUserExist(String account, String password){
        return userService.isUserExist(account,password);
    }

    @GetMapping("/getUserInfoByUserUUID")
    @ResponseBody
    public UserInfo getUserInfoByUserUUID(String uuid){
        return userService.getUserInfoByUserUUID(uuid);
    }

    @GetMapping("/getUserInfo")
    @ResponseBody
    public UserInfo getUserInfo(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        return userService.getUserInfoByUserUUID(userUUID);
    }

    @PostMapping("/updateUserInfo")
    @ResponseBody
    public ChamSysCodeEnum updateUserInfo(@RequestBody UserInfo userInfo){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        userInfo.setUserUuid(userUUID);
        return userService.updateUserInfo(userInfo);
    }


    @PostMapping("/updateHeadImg")
    @ResponseBody
    public ChamSysCodeEnum updateHeadImg(@RequestBody UserInfo userInfo){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String token = request.getHeader("token");
        String userUUID = TokenUtils.getLoginUserUUID(token);
        userInfo.setUserUuid(userUUID);
        return userService.updateHeadImg(userInfo);
    }


}
